package org.wisdom.data.jpa.entity;

import lombok.Data;
import lombok.EqualsAndHashCode;
import org.hibernate.annotations.Filter;
import org.hibernate.annotations.FilterDef;
import org.hibernate.annotations.Filters;
import org.hibernate.annotations.ParamDef;
import org.springframework.core.annotation.Order;
import org.springframework.data.annotation.CreatedBy;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import org.wisdom.data.jpa.support.WisdomEntityListener;

import javax.persistence.*;
import java.util.Date;

/**
 * 公共实体
 *
 * @author 张智研
 * @date 2022/05/19 13:21
 **/

@EqualsAndHashCode(callSuper = true)
@FilterDef(name = "filterByCompanyId", parameters = {
        // org.hibernate.type.BasicTypeRegistry 数据类型定义类
        @ParamDef(name = "filterCode", type = "string")
})
@FilterDef(name = "filterInCompanyId", parameters = {
        @ParamDef(name = "filterCode", type = "string")
})
@FilterDef(name = "filterByDeptId", parameters = {
        @ParamDef(name = "filterCode", type = "string")
})
@FilterDef(name = "filterInDeptId", parameters = {
        @ParamDef(name = "filterCode", type = "string")
})
@FilterDef(name = "filterByDeptPath", parameters = {
        @ParamDef(name = "filterCode", type = "string")
})
@FilterDef(name = "filterByCreator", parameters = {
        @ParamDef(name = "filterCode", type = "string")
})
@Filters(value = {
        @Filter(name = "filterByAppId", condition = "c_company_id = :filterCode"),
        @Filter(name = "filterByCompanyId", condition = "c_company_id = :filterCode"),
        @Filter(name = "filterInCompanyId", condition = "c_company_id in (:filterCode)"),
        @Filter(name = "filterByDeptId", condition = "c_dept_id = :filterCode"),
        @Filter(name = "filterInDeptId", condition = "c_dept_id in (:filterCode)"),
        @Filter(name = "filterByDeptPath", condition = "c_dept_id_path like :filterCode"),
        @Filter(name = "filterByCreator", condition = "c_creator = :filterCode")
})
@Data
@MappedSuperclass
@EntityListeners({AuditingEntityListener.class, WisdomEntityListener.class})
public class WisdomEntity extends ObjectEntity {

    /**
     * 创建人,不允许修改
     */
    @Column(updatable = false)
    @CreatedBy
    @Order(10000)
    private String creator;

    /**
     * 创建时间,不允许修改
     */
    @Column(updatable = false)
    @CreatedDate
    @Temporal(TemporalType.TIMESTAMP)
    @Order(10001)
    private Date createTime;

    /**
     * 最后修改者
     */
    @LastModifiedBy
    @Order(10002)
    private String lastModifier;

    /**
     * 最后修改时间
     */
    @LastModifiedDate
    @Temporal(TemporalType.TIMESTAMP)
    @Order(10003)
    private Date lastModifyTime;

    /**
     * 创建人所在主部门ID
     */
    @Column(updatable = false)
    @Order(10004)
    private String createDeptId;

    /**
     * 创建人所在主部门idPath
     */
    @Column(updatable = false)
    @Order(10005)
    private String createDeptIdPath;

    /**
     * 创建人所在公司ID
     */
    @Column(updatable = false)
    @Order(10006)
    private String createCompanyId;

    /**
     * 删除标识
     */
    @Order(10008)
    private Boolean delFlag;

    /**
     * 时间戳
     */
    @Order(10009)
    private Long timeStamp;
}
